/**
 * @author Xiao Xin
 * @version 1.0.0
 * @date 20210309
 * @copyright MIN-Group:国家重大科技基础设施-未来网络北大实验室：深圳市信息论与未来网络重点实验室
 */

 var SignatureInfo = require('./SignatureInfo').SignatureInfo
 var SignatureValue = require ('./SignatureValue').SignatureValue
 var createSignatureInfoBySignatureTypeValueAndKeyLocator = require('./SignatureInfo').createSignatureInfoBySignatureTypeValueAndKeyLocator
 var createSignatureValueByByteArray = require('./SignatureValue').createSignatureValueByByteArray
 var createKeyLocateByString = require("./KeyLocator").createKeyLocatorByString
 /**
  * @description 存储一个签名的相关信息，包括签名的元数据和签名值
  */
 class Signature{
   constructor(){
     this.SigInfo = new SignatureInfo()
     this.SigValue = new SignatureValue()
   }
 }

/**
 * @description 根据网络身份创建一个 Signature
 * @param {Identity} identity
 * @param {Uint8Array} value
 * @return {Signature}
 */
function createSignatureByIdentifyAndValue(identity,value){
  //往包中添加签名
  //todo keylocator
  var signature = new Signature()
  try {
    var keyLocator = createKeyLocateByString("/"+ identity.Name)
    var sigInfo = createSignatureInfoBySignatureTypeValueAndKeyLocator(identity.KeyParam.SignatureAlgorithm,keyLocator)
    signature.SigInfo = sigInfo
    var sigValue = createSignatureValueByByteArray(value)
    signature.SigValue = sigValue
  } catch (error) {
    throw error
  }
  return signature
}

module.exports = {
  Signature: Signature,
  createSignatureByIdentifyAndValue: createSignatureByIdentifyAndValue
}